<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class menu extends MX_Controller
{
	function menu()
	{
		parent::__construct();
		$this->check_login();
		$this->load->model('mod_lib');
	}
	
	function index()
	{
		$_SESSION['select_id']		= NULL;
		$_SESSION['name']			= NULL;
		$data['title'] 			= "Quản Lý Menu Admin";
		$this->load->library('pagination');
		$per_page 				= 30;
		$total 	  				= $this->mod_lib->count('admin_menu', array());
		$data['menu_admin'] 	= $this->mod_lib->load_all('admin_menu', '', array(), $per_page, $this->uri->segment(4), array("cat_id"=>"ASC", 'order'=>'ASC', 'name'=>'ASC'));
		$base_url				= site_url('admin_menu/menu/index');
		$config['base_url']		= $base_url;
		$config['per_page']		= $per_page;
		$config['total_rows']	= $total;
		$config['uri_segment']	= 4;
		$data['rs']				= $this->uri->segment(4);
		$this->pagination->initialize($config);
		$data['paging']			= $this->pagination->create_links();
		$data['content_admin']	= 'index';
		$this->load->view('back_end/template_admin', $data);
	}
	
	function search()
	{
		
	}
	
	function add($id="")
	{
		if ($id == "")
		{
			$data['title'] 	= "Thêm Mới Menu Admin";
			
			$this->db->select_max('order', 'menu_order');
			$query 	= $this->db->get('admin_menu')->row_array();
			$order	= $query['menu_order'] + 1;
			
			if ($this->input->post('cmd'))
			{
				$result		= array(
									'cat_id'   		=> $this->input->post('cat_id'),
									'name' 			=> trim($this->input->post('name')),
									'createdate'	=> time(),
									'updatedate'	=> time(),
									'order'			=> $order,
									'module'		=> trim($this->input->post('module')),	
									'link'			=> trim($this->input->post('link')),
									'status'   		=> $this->input->post('status')
				);
				$query	= $this->db->get_where('admin_menu', array('module'=>strtolower(trim($this->input->post('module')))));
				if ($query->num_rows() > 0)
				{
					$_SESSION['error_add']	= 'Tên Menu này đã tồn tại. Vui lòng chọn Tên Menu khác';
					redirect(base_url().'admin_menu/menu/add');
				}
				else
				{
					$this->db->insert('admin_menu', $result);
					
					$this->db->select_max('id', 'menu_id');
					$query 	= $this->db->get('admin_menu')->row_array();
					$max_id	= $query['menu_id'] + 1;
					
					$group_admin 	= $this->mod_lib->load_all('admin_groups', '', array(), '', '', array("id"=>"desc"));
					foreach ($group_admin as $gra)
					{
						$groups_admin_id	= $gra['id'];
						$data3	= array(
								'menu_admin_id' 			=> $max_id,
								'access' 					=> 0,
								'add'   					=> 0,
								'edit'   					=> 0,
								'del'						=> 0,
								'group_admin_id'   			=> $groups_admin_id
						);
						$this->db->insert('admin_permiss', $data3);
					}
					
					$module_input	= trim($this->input->post('module'));
					$link			= trim($this->input->post('link'));
					
					if (!empty($module_input))
					{
						$module	= getcwd().'/application/modules/'.$module_input;
						if(!is_dir($module))
						{
							mkdir($module, 0777, true);
							$controller	= $module.'/controllers';
							$models		= $module.'/models';
							$views		= $module.'/views';
							if (!is_dir($controller))
							{
								mkdir($controller, 0777, true);			
								$filename	= $controller.'/'.(!empty($link)?$link:$module_input).'.php';
								if (!file_exists($filename))
								{
									is_file($filename);
									$fp = fopen($filename, 'w');
									fwrite($fp, "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n");
									fwrite($fp, "class ".(!empty($link)?$link:$module_input)." extends MX_Controller\n");
									fwrite($fp, "{\n");
										fwrite($fp, "\t"."function ".(!empty($link)?$link:$module_input)."()\n");
										fwrite($fp, "\t"."{\n");
											fwrite($fp, "\t\t"."parent::__construct();\n");
											fwrite($fp, "\t\t".'$this->check_login();'."\n");
										fwrite($fp, "\t"."}\n\n");
										
										fwrite($fp, "\t"."function index()\n");
										fwrite($fp, "\t"."{\n");							
											fwrite($fp, "\t\t".'$data[\'title\'] = \''.(!empty($link)?$link:$module_input).'\';'."\n");
											fwrite($fp, "\t\t".'$data[\'content_admin\'] = \'index\';'."\n");
											fwrite($fp, "\t\t".'$this->load->view(\'back_end/template_admin\', $data);'."\n");
										fwrite($fp, "\t"."}\n\n");
		
										fwrite($fp, "\t"."function add(\$id = \"\")\n");
										fwrite($fp, "\t"."{\n");
											fwrite($fp, "\t\t"."if (\$id == \"\")\n");
											fwrite($fp, "\t\t"."{\n");
												fwrite($fp, "\t\t\t".'$data[\'title\'] = \'Add '.(!empty($link)?$link:$module_input).'\';'."\n");
												fwrite($fp, "\t\t\t".'$data[\'content_admin\'] = \'add\';'."\n");
											fwrite($fp, "\t\t"."}\n");
											fwrite($fp, "\t\t"."else\n");
											fwrite($fp, "\t\t"."{\n");
												fwrite($fp, "\t\t\t".'$data[\'title\'] = \'Update '.(!empty($link)?$link:$module_input).'\';'."\n");
												fwrite($fp, "\t\t\t".'$data[\'content_admin\'] = \'edit\';'."\n");
											fwrite($fp, "\t\t"."}\n");
											fwrite($fp, "\t\t".'$this->load->view(\'back_end/template_admin\', $data);'."\n");
										fwrite($fp, "\t"."}\n\n");
		
										fwrite($fp, "\t"."function delete(\$id = \"\")\n");
										fwrite($fp, "\t"."{\n");
											fwrite($fp, "\t\t".'$data[\'title\'] = \'Delete '.(!empty($link)?$link:$module_input).'\';'."\n");
											fwrite($fp, "\t\t".'redirect(base_url().\''.$module_input.'/'.$link.'\');'."\n");
										fwrite($fp, "\t"."}\n");															
										
									fwrite($fp, "}");
									fclose($fp);
								}			
							}
							if (!is_dir($models))
							{
								mkdir($models, 0777, true);
								$filename	= $models.'/'.(!empty($link)?$link:$module_input).'.php';
								if (!file_exists($filename))
								{
									is_file($filename);
									$fp = fopen($filename, 'w');
									fwrite($fp, "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');");
									fclose($fp);
								}
							}
							if (!is_dir($views))
							{
								mkdir($views, 0777, true);
								$filename_index	= $views.'/'.'index.php';
								$filename_add	= $views.'/'.'add.php';
								$filename_edit	= $views.'/'.'edit.php';
								if (!file_exists($filename_index))
								{
									is_file($filename_index);	
									$fp = fopen($filename_index, 'w');
									fclose($fp);
								}												
								if (!file_exists($filename_add))
								{
									is_file($filename_add);	
									$fp = fopen($filename_add, 'w');
									fclose($fp);
								}												
								if (!file_exists($filename_edit))
								{
									is_file($filename_edit);	
									$fp = fopen($filename_edit, 'w');
									fclose($fp);
								}												
							}										
						}
						else
						{
							
						}				
					}
					if ($this->input->post('cmd')	== 'Add')
					{
						echo '<script>alert("Bạn đã thêm mới thành công một menu user."); window.location = "'.base_url().'admin_menu/admin_menu"</script>';
					}
					else if ($this->input->post('cmd')	== 'Save & Add')
					{
						redirect(base_url().'admin_menu/menu/add');
					}
				}
			}
			$data['content_admin']	= 'add';
		}
		else
		{
			$data['title']				= 'Update Admin Menu';
			$data['admin_menu_edit']	= $this->mod_lib->detail('admin_menu', array("id"=>$id));
			if ($this->input->post('cmd'))
			{
				$result		= array(
									'cat_id'   		=> $this->input->post('cat_id'),
									'name' 			=> trim($this->input->post('name')),
									'updatedate'	=> time(),
									'module'		=> trim($this->input->post('module')),	
									'link'			=> trim($this->input->post('link')),
									'status'   		=> $this->input->post('status')
				);
				$this->mod_lib->update('admin_menu', array("id"=>$id), $result);
				
				$module_input	= trim($this->input->post('module'));
				$link			= trim($this->input->post('link'));
					
				if (!empty($module_input))
				{
					$module	= getcwd().'/application/modules/'.$module_input;
					if(!is_dir($module))
					{
						mkdir($module, 0777, true);
						$controller	= $module.'/controllers';
						$models		= $module.'/models';
						$views		= $module.'/views';
						if (!is_dir($controller))
						{
							mkdir($controller, 0777, true);
							$filename	= $controller.'/'.(!empty($link)?$link:$module_input).'.php';
							if (!file_exists($filename))
							{
								is_file($filename);
								$fp = fopen($filename, 'w');
								fwrite($fp, "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n");
								fwrite($fp, "class ".(!empty($link)?$link:$module_input)." extends MX_Controller\n");
								fwrite($fp, "{\n");
								fwrite($fp, "\t"."function ".(!empty($link)?$link:$module_input)."()\n");
								fwrite($fp, "\t"."{\n");
								fwrite($fp, "\t\t"."parent::__construct();\n");
								fwrite($fp, "\t\t".'$this->check_login();'."\n");
								fwrite($fp, "\t"."}\n\n");
				
								fwrite($fp, "\t"."function index()\n");
								fwrite($fp, "\t"."{\n");
								fwrite($fp, "\t\t".'$data[\'title\'] = \''.(!empty($link)?$link:$module_input).'\';'."\n");
								fwrite($fp, "\t\t".'$data[\'content_admin\'] = \'index\';'."\n");
								fwrite($fp, "\t\t".'$this->load->view(\'back_end/template_admin\', $data);'."\n");
								fwrite($fp, "\t"."}\n\n");
				
								fwrite($fp, "\t"."function add(\$id = \"\")\n");
								fwrite($fp, "\t"."{\n");
								fwrite($fp, "\t\t"."if (\$id == \"\")\n");
								fwrite($fp, "\t\t"."{\n");
								fwrite($fp, "\t\t\t".'$data[\'title\'] = \'Add '.(!empty($link)?$link:$module_input).'\';'."\n");
								fwrite($fp, "\t\t\t".'$data[\'content_admin\'] = \'add\';'."\n");
								fwrite($fp, "\t\t"."}\n");
								fwrite($fp, "\t\t"."else\n");
								fwrite($fp, "\t\t"."{\n");
								fwrite($fp, "\t\t\t".'$data[\'title\'] = \'Update '.(!empty($link)?$link:$module_input).'\';'."\n");
								fwrite($fp, "\t\t\t".'$data[\'content_admin\'] = \'edit\';'."\n");
								fwrite($fp, "\t\t"."}\n");
								fwrite($fp, "\t\t".'$this->load->view(\'back_end/template_admin\', $data);'."\n");
								fwrite($fp, "\t"."}\n\n");
				
								fwrite($fp, "\t"."function delete(\$id = \"\")\n");
								fwrite($fp, "\t"."{\n");
								fwrite($fp, "\t\t".'$data[\'title\'] = \'Delete '.(!empty($link)?$link:$module_input).'\';'."\n");
								fwrite($fp, "\t\t".'redirect(base_url().\''.$module_input.'/'.$link.'\');'."\n");
								fwrite($fp, "\t"."}\n");
				
								fwrite($fp, "}");
								fclose($fp);
							}
						}
						if (!is_dir($models))
						{
							mkdir($models, 0777, true);
							$filename	= $models.'/'.(!empty($link)?$link:$module_input).'.php';
							if (!file_exists($filename))
							{
								is_file($filename);
								$fp = fopen($filename, 'w');
								fwrite($fp, "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');");
								fclose($fp);
							}
						}
						if (!is_dir($views))
						{
							mkdir($views, 0777, true);
							$filename_index	= $views.'/'.'index.php';
							$filename_add	= $views.'/'.'add.php';
							$filename_edit	= $views.'/'.'edit.php';
							if (!file_exists($filename_index))
							{
								is_file($filename_index);
								$fp = fopen($filename_index, 'w');
								fclose($fp);
							}
							if (!file_exists($filename_add))
							{
								is_file($filename_add);
								$fp = fopen($filename_add, 'w');
								fclose($fp);
							}
							if (!file_exists($filename_edit))
							{
								is_file($filename_edit);
								$fp = fopen($filename_edit, 'w');
								fclose($fp);
							}
						}
					}
					else
					{
							
					}
				}
			}
			$data['content_admin']	= 'edit';			
		}
		$data['menu_admin']		= $this->mod_lib->load_all('admin_menu', '', array('cat_id'=>0), '', '', array('name'=>'ASC'));
		$this->load->view('back_end/template_admin', $data);
	}
	
	function delete($id="")
	{
		$var	= getcwd();
		if (empty($id))
		{
			$id			= $this->input->post('chon');
			$id			= substr($id, 0, strlen($id)-1);
			$mang_id	= explode(',', $id);
			if (is_array($mang_id))
			{
				foreach ($mang_id as $value_id)
				{
					$rs		= $this->mod_lib->detail('admin_menu', array('id'=>$value_id));
					$module	= $rs[0]['module'];
					$link	= $rs[0]['link'];
					
					if (!empty($module))
					{
						$dir	= $var.'/application/modules/'.$module.'/';
						$controllers	= $dir.'controllers';
						$models			= $dir.'models';
						$views			= $dir.'views';
					
						if (is_dir($controllers))
							$this->rrmdir($controllers);
						if (is_dir($models))
							$this->rrmdir($models);
						if (is_dir($views))
							$this->rrmdir($views);
						if (is_dir($dir))
							$this->rrmdir($dir);
					}
				}
			}
			$this->mod_lib->delete('admin_menu', $mang_id, 'id', true);
			$this->mod_lib->delete('admin_permiss', $mang_id, 'menu_admin_id');
		}
		else
		{
			$rs		= $this->mod_lib->detail('admin_menu', array('id'=>$id));
			$module	= $rs[0]['module'];
			$link	= $rs[0]['link'];
			
			if (!empty($module))
			{
				$dir	= $var.'/application/modules/'.$module.'/';
				$controllers	= $dir.'controllers';
				$models			= $dir.'models';
				$views			= $dir.'views';
				
				if (is_dir($controllers))
					$this->rrmdir($controllers);
				if (is_dir($models))
					$this->rrmdir($models);
				if (is_dir($views))
					$this->rrmdir($views);
				if (is_dir($dir))
					$this->rrmdir($dir);								
			}
			$this->mod_lib->delete('admin_menu', $id, 'id');			
			$this->mod_lib->delete('admin_permiss', $id, 'menu_admin_id');			
		}
		redirect(base_url().'admin_menu/menu');
	}
}